from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import xlsxwriter, time

browser = webdriver.Chrome()
browser.maximize_window()
browser.get('https://python123.io/index/login')

'''
# 扫码登陆
browser.find_element(By.XPATH, '//button[@class="button is-primary is-outlined is-fullwidth"]/i').click()
WebDriverWait(browser, 30).until(EC.text_to_be_present_in_element((By.CLASS_NAME, 'is-text-small'), '我的主页'))
'''

# cookie 登录
browser.delete_all_cookies()
browser.add_cookie({'domain': 'python123.io', 'httpOnly': False, 'name': 'token', 'path': '/', 'secure': False, 'value': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7ImVtYWlsIjoiNTc3MTM1QHB5dGhvbjEyMyIsIm5hbWUiOiJNci4gICBUaWFuIiwiaWQiOiI1NzcxMzUiLCJvcGVuX2lkIjoid2VjaGF0Om94WjlSMGU0R082c0FPWjJVem50VnVUMlNwNTAiLCJyb2xlcyI6WyJzdHVkZW50Il0sImxhc3RfbG9naW4iOjE2NDgxMTg1OTYyMDV9LCJpYXQiOjE2NDgxMTg2MzQsImV4cCI6MTY0OTQxNDYzNH0.FvakEm9U3yPas4Pz39T7NUeSDSVtDzEVexKXPu9ZzMs'})
browser.get('https://python123.io/student/series/12/')
browser.implicitly_wait(5)

catalogs = browser.find_elements(By.XPATH, '//div[@class="catalog"]')[1:]
d = {}
for catalog in catalogs:
    catalog_head = catalog.find_element(By.CLASS_NAME, 'heading').text
    node_a = catalog.find_elements(By.XPATH, './div/a')
    items = []
    for a in node_a:
        name = a.find_element(By.XPATH, './/div[@class="media-content"]/b').text
        url = a.get_attribute('href')
        items.append((name, url))
    d[catalog_head] = items

'''
d 的值：
{'模拟试卷': [('模拟试卷 A', 'https://python123.io/student/series/12/modules/149'), ('模拟试卷 B', 'https://python123.io/student/series/12/modules/150'), ('模拟试卷 C', 'https://python123.io/student/series/12/modules/151'), ('模拟试卷 D', 'https://python123.io/student/series/12/modules/152'), ('模拟试卷 E', 'https://python123.io/student/series/12/modules/153')], '公共基础知识题库': [('数据结构与算法', 'https://python123.io/student/series/12/modules/171'), ('程序设计基础', 'https://python123.io/student/series/12/modules/172'), ('软件工程基础', 'https://python123.io/student/series/12/modules/173'), ('计算机系统', 'https://python123.io/student/series/12/modules/170'), ('数据库设计基础', 'https://python123.io/student/series/12/modules/174')], 'PYTHON 单选题库': [('Python 语法基础', 'https://python123.io/student/series/12/modules/159'), ('基本数据类型', 'https://python123.io/student/series/12/modules/160'), ('程序的控制结构', 'https://python123.io/student/series/12/modules/161'), ('函数和代码的复用', 'https://python123.io/student/series/12/modules/162'), ('组合数据类型', 'https://python123.io/student/series/12/modules/163'), ('文件与数据格式格化', 'https://python123.io/student/series/12/modules/164'), ('Python基础生态', 'https://python123.io/student/series/12/modules/165'), ('Python计算生态', 'https://python123.io/student/series/12/modules/166')], 'PYTHON 编程题库': [('基本编程题', 'https://python123.io/student/series/12/modules/167'), ('简单应用题', 'https://python123.io/student/series/12/modules/168'), ('综合应用题', 'https://python123.io/student/series/12/modules/169')], '考前冲刺': [('考前冲刺试卷 F', 'https://python123.io/student/series/12/modules/175'), ('考前冲刺试卷G', 'https://python123.io/student/series/12/modules/176'), ('考前冲刺试卷H', 'https://python123.io/student/series/12/modules/177')]}
'''

def c_pool(part):
# 爬取选择题题库
    wb = xlsxwriter.Workbook('d:/{}.xlsx'.format(part))
    global d
    for item in d[part]:
        ws = wb.add_worksheet(item[0])
        header = {'A1': '序号', 'B1': '题干', 'C1': '选项A', 'D1': '选项B', 'E1': '选项C', 'F1': '选项D', 'G1': '答案'}
        for l in header:
            ws.write(l, d[l])
        n = 2
        browser.get(item[1]+'/choices')
        browser.find_element(By.XPATH, '//div[@class="column is-3"]/div/div/div/div[4]').click()
        node_div = browser.find_elements(By.XPATH, '//div[@class="column is-9"]/div/div/div')
        for div in node_div:
            index = div.find_element(By.CLASS_NAME, 'problem-index').text
            body = div.find_element(By.CLASS_NAME, 'mce-content-body').text
            options = div.find_elements(By.XPATH, './/div[@class="options is-clickable"]/div/div')
            options = [i.text for i in options]
            answer = div.find_element(By.XPATH, './div[2]/div[2]/span[4]').text
            content = [index, body, *options, answer]
            for i in range(7):
                ws.write('ABCDEFG'[i]+str(n), content[i])
            n += 1
    wb.close()

            


c_pool('公共基础知识题库')

c_pool('PYTHON 单选题库')


def p_pool():
    # 爬取简单编程题
    i = 0
    while i < 60:
        titles = browser.find_elements(By.XPATH, '//div[@class="card-content programming-card"]')
        node_b = titles[i].find_elements(By.XPATH, './/div[@class="media-content"]/b')
        name = ''.join([name.text for name in node_b])
        titles[i].click()
        browser.implicitly_wait(1)
        try:
            browser.find_elements(By.XPATH, '//div[@class="card-content condense vertical-align has-border-bottom"]/div/a')[3].click()
        except IndexError:
            browser.find_elements(By.XPATH, '//div[@class="card-content condense vertical-align has-border-bottom"]/div/a')[2].click()
        time.sleep(1)
        browser.execute_script('window.scrollTo(0,120)')
        browser.save_screenshot('D:\基本编程题\{}.png'.format(name))
        browser.back()
        i += 1

p_pool()



'''

def c_pool():
# 爬取选择题题库


def p_pool():
# 爬取编程题题库


def paper():
# 爬取模拟试卷和考前冲刺


'''